package com.cskaoyan.wordmemorize.controller.admin;

import com.cskaoyan.wordmemorize.auth.StpKit;
import com.cskaoyan.wordmemorize.common.result.Result;
import com.cskaoyan.wordmemorize.dto.admin.DeleteEmployeeRoleOperateDTO;
import com.cskaoyan.wordmemorize.dto.admin.EmployeeDTO;
import com.cskaoyan.wordmemorize.dto.admin.PageDTO;
import com.cskaoyan.wordmemorize.request.EmployeeCommand;
import com.cskaoyan.wordmemorize.request.PageRequest;
import com.cskaoyan.wordmemorize.request.RoleCommand;
import com.cskaoyan.wordmemorize.service.EmployeeService;
import com.cskaoyan.wordmemorize.service.FileUploadService;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

@RestController
@RequestMapping("admin")
public class EmployeeServiceController {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    FileUploadService fileUploadService;

    @PostMapping("employee")
    public Result addEmployee(@RequestBody EmployeeCommand employeeCommand) {
        employeeService.addEmployee(employeeCommand);
        return Result.ok(null);
    }
    @PostMapping("employee/avatar")
    public Result uploadFile(@RequestBody MultipartFile file) throws IOException {
        String url=fileUploadService.upload(file);
        return Result.ok(url);
    }
    @GetMapping("employee/list")
    public Result<PageDTO<EmployeeDTO>> getEmployeePage(PageRequest pageRequest)  {
        PageDTO<EmployeeDTO> pageDTO=employeeService.getEmployeePage(pageRequest);
        return Result.ok(pageDTO);
    }
    @PutMapping("employee")
    public Result updateEmployee(@RequestBody EmployeeCommand employeeCommand)  {
        employeeService.updateEmployee(employeeCommand);
        return Result.ok(null);
    }
    @GetMapping("employee/info")
    public Result<EmployeeDTO> getEmployeeInfo()  {
        Long userId = StpKit.ADMIN.getLoginIdAsLong();
        EmployeeDTO employeeDTO=employeeService.getEmployeeInfo(userId);
        return Result.ok(employeeDTO);
    }
    @DeleteMapping("employee/{employeeId}")
    public Result delete(@PathVariable("employeeId")Long id)  {
        employeeService.delete(id);
        return Result.ok(null);
    }
    @DeleteMapping("employee/{employeeId}/role/{roleId}")
    public Result deleteRole(@PathVariable("employeeId")Long employeeId,@PathVariable("roleId")Long roleId)  {
        DeleteEmployeeRoleOperateDTO deleteEmployeeRoleOperateDTO=employeeService.deleteRole(employeeId,roleId);
        return Result.ok(null);
    }

}
